﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

namespace cnetmegamanprj
{
    //http://stackoverflow.com/questions/3396973/scrolling-picturebox-in-net/3399939#3399939
    class ScrollablePictureBox : PictureBox
    {
        public SelectablePictureBox pic = new SelectablePictureBox();
        public PictureBox picCorner = new PictureBox();
        HScrollBar hScroll = new HScrollBar();
        VScrollBar vScroll = new VScrollBar();
        int w, h, scrollAmount;
        public ScrollablePictureBox(int w_, int h_, Image img, int scrollAmount)
        {
            AddControls();
            Set(w_, h_, img, scrollAmount);
        }
        public ScrollablePictureBox()
        {
            AddControls();
        }
        public void ScrollEvent(object sender, MouseEventArgs e)
        {
            if (e.Delta == 0)
                return;

            var v = vScroll.Value + (e.Delta >= 120 ? -scrollAmount : scrollAmount);
            v = v > vScroll.Maximum ? vScroll.Maximum : v;
            v = v < vScroll.Minimum ? vScroll.Minimum : v;
            vScroll.Value = v;
        }
        void AddControls()
        {
            picCorner.BackColor = Color.Gray;
            vScroll.ValueChanged += Vert_EventHandler;

            this.Controls.Add(picCorner);
            this.Controls.Add(vScroll);
            this.Controls.Add(hScroll);
            this.Controls.Add(pic);            
        }
        public void Set(int w_, int h_, Image img, int scrollAmount_)
        {
            w = w_;
            h = h_;
            scrollAmount = scrollAmount_;
            pic.Image = img;
            pic.Width = img.Width;
            pic.Height = img.Height;

            if (w >= img.Width)
            {
                this.Width = w;
                hScroll.Visible = false;
            }
            else
            {
                hScroll.Left = 0;
                hScroll.Top = h;
                hScroll.Width = w;
                hScroll.Minimum = 0;
                hScroll.Maximum = pic.Image.Width - w;
                hScroll.Scroll += hScrollBar1_Scroll;
                hScroll.Visible = true;
            }

            if (h >= img.Height)
            {
                this.Width = w;
                vScroll.Visible = false;
            }
            else
            {
                vScroll.Left = w;
                vScroll.Top = 0;
                vScroll.Height = h;
                vScroll.Minimum = 0;
                vScroll.Maximum = pic.Image.Height - h;
                vScroll.Scroll += vScrollBar1_Scroll;
                vScroll.Visible = true;
            }
            
            this.Width = vScroll.Visible ? w + vScroll.Width : w;
            this.Height = hScroll.Visible ? h + hScroll.Height: h;

            picCorner.Left = vScroll.Left;
            picCorner.Width = vScroll.Width;
            picCorner.Top = hScroll.Top;
            picCorner.Height = hScroll.Height;
            picCorner.BackColor = this.BackColor;
            picCorner.Visible = (vScroll.Visible && hScroll.Visible);
        }
        void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            pic.Left = -e.NewValue;
        }
        void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            pic.Top = -e.NewValue;
        }
        void Vert_EventHandler(object sender, EventArgs e)
        {
            pic.Top = -vScroll.Value;
        }
    }
    class SelectablePictureBox : PictureBox
    {
        public SelectablePictureBox()
        {
            SetStyle(ControlStyles.Selectable, true);
            SetStyle(ControlStyles.UserMouse, true);
            this.TabStop = true;
            this.MouseDown += MouseEventHandler;
        }

        public KeyEventHandler KeyDown; //this is a hack since. It should inherit from Control but PictureBox ruins it.
        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (KeyDown != null) KeyDown(this, e);
        } 

        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
        public new int TabIndex
        {
            get { return base.TabIndex; }
            set { base.TabIndex = value; }
        }

        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
        public new bool TabStop
        {
            get { return base.TabStop; }
            set { base.TabStop = value; }
        }
        protected override bool IsInputKey(Keys keyData)
        {
            if (keyData == Keys.Up || keyData == Keys.Down ||
                keyData == Keys.Left || keyData == Keys.Right) return true;
            return base.IsInputKey(keyData);
        }
        protected void MouseEventHandler(object sender, MouseEventArgs e)
        {
            if (e.Button != 0)
                ((Control)sender).Focus();
        }
    }
}
